package com.trinity.web.controller.payment.unique;

import com.trinity.common.annotation.Log;
import com.trinity.common.core.controller.BaseController;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.core.page.TableDataInfo;
import com.trinity.common.enums.BusinessType;
import com.trinity.common.utils.poi.ExcelUtil;
import com.trinity.orders.service.IXcOrdersService;
import com.trinity.uniquefees.domain.UfBookList;
import com.trinity.uniquefees.service.IUfBookListService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数目清单Controller
 *
 * @author 董诗攀
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/uniquefees/bookList")
public class UfBookListController extends BaseController {
    @Resource
    private IUfBookListService ufBookListService;

    @Resource
    private IXcOrdersService ordersService;

    /**
     * 查询数目清单列表
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:bookList:list')")
    @GetMapping("/list")
    public TableDataInfo list(UfBookList ufBookList) {
        startPage();
        List<UfBookList> list = ufBookListService.selectUfBookListList(ufBookList);
        return getDataTable(list);
    }

    /**
     * 导出数目清单列表
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:bookList:export')")
    @Log(title = "数目清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UfBookList ufBookList) {
        List<UfBookList> list = ufBookListService.selectUfBookListList(ufBookList);
        ExcelUtil<UfBookList> util = new ExcelUtil<UfBookList>(UfBookList.class);
        util.exportExcel(response, list, "数目清单数据");
    }

    /**
     * 获取数目清单详细信息
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:bookList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(ufBookListService.selectUfBookListById(id));
    }

    /**
     * 新增数目清单
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:bookList:add')")
    @Log(title = "数目清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UfBookList ufBookList) {
        return ufBookListService.createdUfBookList(ufBookList);
    }

    /**
     * 修改数目清单
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:bookList:edit')")
    @Log(title = "数目清单", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody UfBookList ufBookList) {
        return toAjax(ufBookListService.updateUfBookList(ufBookList));
    }

    /**
     * 删除数目清单
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:bookList:remove')")
    @Log(title = "数目清单", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}/delete")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(ufBookListService.deleteUfBookListByIds(ids));
    }

    /**
     * 切换项目状态
     * 未开始 --激活-> 进行中
     * 进行中 --暂停-> 未开始
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:bookList:edit')")
    @Log(title = "切换项目状态", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/changeStatus", method = {RequestMethod.POST})
    public AjaxResult changeStatus(@RequestBody UfBookList ufBookList) {
        try {
            //改变项目状态
            ufBookListService.updateUfBookList(ufBookList);
            //改变订单状态
            ordersService.changeStatus(ufBookList.getId(), ufBookList.getStatus());
        }catch (Exception e){
            return AjaxResult.error("更改失败！");
        }
        return success();
    }
}
